<template>
  <div>
    <el-dialog
      :title="addInfo.isAdd ? '添加轮播图列表' : '编辑轮播图列表'"
      :visible.sync="addInfo.flag"
      center
      :before-close="cancel"
    >
      <el-form
        :model="ruleForm"
        ref="ruleForm"
        :rules="rules"
        :label-width="formLabelWidth"
      >
        <el-form-item label="标题" prop="title" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="ruleForm.title"></el-input>
        </el-form-item>

        <el-form-item label="图片" prop="img" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-preview="onPreview"
            :on-remove="handleRemove"
            :auto-upload="false"
            :on-change="onChange"
            :file-list="fileList"
            :limit="1"
            :on-exceed="onExceed"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
            v-model="ruleForm.status"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { banneredit, banneradd, bannerinfo } from "../../request/api";
export default {
  
  data() {
    return {
      formLabelWidth: "120px",
      ruleForm: {
        title: "",
        img: "",
        status: 1,
      },
      dialogVisible: false,
      //预览时调用的图片地址
      dialogImageUrl: "",
      fileList: [], //用于图片回显
      rules: {
        title: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["addInfo"],
  methods: {
    ...mapActions({
      getBnnerAction: "banner/getBnnerAction",
    }),
    //文件限制方法
    onExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    //预览的回调函数
    onPreview(file) {
      console.log(file, "文件");
      //得到文件地址之后，把它赋值给要显示的属性
      this.dialogImageUrl = file.url;
      //打开图片预览弹框
      this.dialogVisible = true;
    },
    //移除触发的钩子函数
    handleRemove(file, fileList) {
      console.log(file, "移除", fileList);
    },
    //文件发生变化触发的回调函数
    onChange(file, fileList) {
      //把文件详情赋值给图片地址l
      this.ruleForm.img = file.raw;
    },
    //清空表单
    rmove() {
      this.ruleForm = {
        title: "",
        img: "",
        status: 1,
      };
      this.$refs.ruleForm.resetFields();
      this.fileList = [];
    },
    //点击取消
    cancel() {
      this.rmove();
      this.$emit("cancel", false);
    },
    //封装一个函数用来数据回显
    lookup(id) {
      bannerinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.ruleForm = res.list;
          //对图片数据进行回显
          this.fileList = this.ruleForm.img
            ? [{ url: "http://localhost:3000" + this.ruleForm.img }]
            : [];
          this.ruleForm.id = id;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //点击确认
    confirm() {
      this.$refs.ruleForm.validate((val) => {
        if (val) {
          if (this.addInfo.isAdd) {
            banneradd(this.ruleForm).then((res) => {
              if (res.code == 200) {
                //清空表单函数
                this.rmove();
                //调用列表函数
                this.getBnnerAction();
                //调用关闭函数
                this.cancel();
                this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //编辑接口
            banneredit(this.ruleForm).then((res) => {
              if (res.code == 200) {
                this.rmove();
                //调用关闭函
                //调用列表函数
                this.getBnnerAction();
                this.cancel();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.ipt_info {
  width: 75%;
}
</style>